package com.hx.voice.recognition.auth.common.util.jwt;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 * @descripe：rsa 公钥密钥生成
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/10 14:44
 * @version：V1.0
 */
public class RsaKeyHelper {

    /**
     * @description 获取公钥
     * @method  getPublicKey
     * @param  [filename]
     * @return java.security.PublicKey
     * @date: 2019/6/10 14:45
     * @author:zhoujinbing
     */
    public PublicKey getPublicKey(String filename) throws Exception
    {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
        DataInputStream dataInputStream = new DataInputStream((resourceAsStream));
        byte[] bytes = new byte[resourceAsStream.available()];
        dataInputStream.readFully(bytes);
        dataInputStream.close();
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(bytes);
        KeyFactory rsa = KeyFactory.getInstance("RSA");
        return rsa.generatePublic(x509EncodedKeySpec);
    }

    /**
     * @description 获取私钥
     * @method  getPrivateKey
     * @param  [filename]
     * @return java.security.PrivateKey
     * @date: 2019/6/10 14:59
     * @author:zhoujinbing
     */
    public PrivateKey getPrivateKey(String filename) throws Exception
    {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
        DataInputStream dataInputStream = new DataInputStream((resourceAsStream));
        byte[] bytes = new byte[resourceAsStream.available()];
        dataInputStream.readFully(bytes);
        dataInputStream.close();
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
        KeyFactory rsa = KeyFactory.getInstance("RSA");
        return rsa.generatePrivate(keySpec);
    }

    /**
     * @description 获取公钥
     * @method  getPublicKey
     * @param  [publicKey]
     * @return java.security.PublicKey
     * @date: 2019/6/10 15:02
     * @author:zhoujinbing
     */
    public PublicKey getPublicKey(byte[] publicKey) throws Exception
    {
        X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
        KeyFactory rsa = KeyFactory.getInstance("RSA");
        return rsa.generatePublic(spec);
    }

    /**
     * @description 生成私钥
     * @method  getPrivateKey
     * @param  [privateKey]
     * @return java.security.PrivateKey
     * @date: 2019/6/10 15:03
     * @author:zhoujinbing
     */
    public PrivateKey getPrivateKey(byte[] privateKey) throws Exception
    {
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory rsa = KeyFactory.getInstance("RSA");
        return rsa.generatePrivate(spec);
    }

    /**
     * @description 根据盐生成公钥、私钥
     * @method  generateKey
     * @param  [publicKeyFilename, privateKeyFilename, password]
     * @return void
     * @date: 2019/6/10 15:05
     * @author:zhoujinbing
     */
    public void generateKey(String publicKeyFilename,String privateKeyFilename,String password) throws Exception
    {
        KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
        SecureRandom secureRandom = new SecureRandom(password.getBytes());
        rsa.initialize(1024,secureRandom);
        KeyPair keyPair = rsa.genKeyPair();

        byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
        FileOutputStream fos = new FileOutputStream(publicKeyFilename);
        fos.write(publicKeyBytes);
        fos.close();

        byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
        fos = new FileOutputStream(privateKeyFilename);
        fos.write(privateKeyBytes);
        fos.close();
    }

    /**
     * @description 生成rsa公钥
     * @method  generatePublickey
     * @param  [password]
     * @return byte[]
     * @date: 2019/6/10 15:09
     * @author:zhoujinbing
     */
    public static byte[] generatePublickey(String password) throws IOException,NoSuchAlgorithmException
    {
        KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
        SecureRandom secureRandom = new SecureRandom(password.getBytes());
        rsa.initialize(1024,secureRandom);
        KeyPair keyPair = rsa.genKeyPair();
        return keyPair.getPublic().getEncoded();
    }

    /**
     * @description 生成rsa私钥
     * @method  generatePrivateKey
     * @param  [password]
     * @return byte[]
     * @date: 2019/6/10 15:09
     * @author:zhoujinbing
     */
    public static byte[] generatePrivateKey(String password) throws IOException,NoSuchAlgorithmException
    {
        KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
        SecureRandom secureRandom = new SecureRandom(password.getBytes());
        rsa.initialize(1024,secureRandom);
        KeyPair keyPair = rsa.genKeyPair();
        return keyPair.getPrivate().getEncoded();
    }

    /**
     * @description 根据盐生成公钥、私钥
     * @method  generateKey
     * @param  [password]
     * @return java.util.Map<java.lang.String,byte[]>
     * @date: 2019/6/10 15:12
     * @author:zhoujinbing
     */
    public static Map<String,byte[]> generateKey(String password) throws IOException,NoSuchAlgorithmException
    {
        KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
        SecureRandom secureRandom = new SecureRandom(password.getBytes());
        rsa.initialize(1024,secureRandom);
        KeyPair keyPair = rsa.genKeyPair();

        byte[] publicKey = keyPair.getPublic().getEncoded();
        byte[] privateKey = keyPair.getPrivate().getEncoded();

        Map<String, byte[]> keyMap = new HashMap<>();
        keyMap.put("pub",publicKey);
        keyMap.put("pri",privateKey);
        return keyMap;
    }

    /**
     * @description 给字节base64加密
     * @method  toHexString
     * @param  [b]
     * @return java.lang.String
     * @date: 2019/6/10 15:17
     * @author:zhoujinbing
     */
    public static String toHexString(byte[] b)
    {
        return (new BASE64Encoder().encodeBuffer(b));
    }

    public static final byte[] toBytes(String s) throws IOException
    {
        return (new BASE64Decoder()).decodeBuffer(s);
    }

    /**
     * @description 测试rsa公钥生成
     * @method  main
     * @param  [args]
     * @return void
     * @date: 2019/6/10 15:22
     * @author:zhoujinbing
     */
    public static void main(String[] args) throws NoSuchAlgorithmException {
        KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
        SecureRandom secureRandom = new SecureRandom("123".getBytes());
        rsa.initialize(1024,secureRandom);
        KeyPair keyPair = rsa.genKeyPair();
        System.out.println(keyPair.getPublic().getEncoded());

    }











}